package com.diandian.backend.entrance.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.diandian.backend.entrance.client.DianDianThirdClient;
import com.diandian.backend.entrance.client.dto.third.SkuStockQueryParamDTO;
import com.diandian.backend.entrance.constants.EntranceConstant;
import com.diandian.backend.entrance.dto.ClientPermissionCacheDataDTO;
import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.base.boot.starter.response.BaseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RouterThirdService {

    private static final Logger logger = LoggerFactory.getLogger(RouterThirdService.class);

    @Autowired
    private DianDianThirdClient dianDianThirdClient;

    public Object execute(ClientPermissionCacheDataDTO clientInfoDTO, String clientKey, String clientSecret, String cmd, JSONObject data) {

        // 查询SKU库存数据
        if(EntranceConstant.ROUTER_THIRD_LING_FANG_QUERY_SKU_STOCK.equals(cmd)){

            logger.info("查询SKU库存数据请求，参数: {}", data.toJSONString());

            // 获取配置信息
            String belong = clientInfoDTO.getSettingValueFromSetting(EntranceConstant.SETTING_TYPE_CLIENT, EntranceConstant.SETTING_TYPE_CLIENT_BELONG);
            if(belong == null || "".equals(belong)){
                throw new DefaultException("配置缺失");
            }

            // 参数转换
            SkuStockQueryParamDTO paramDTO = JSON.parseObject(data.toJSONString(), SkuStockQueryParamDTO.class);
            paramDTO.setSource(belong);

            // 执行请求
            BaseResult resultDTO = dianDianThirdClient.skuStockQuery(paramDTO);

            logger.info("根查询SKU库存数据请求结果: {}", JSON.toJSONString(resultDTO));

            // 返回结果
            return resultDTO;
        }

        throw new DefaultException("暂不支持此操作");
    }

}
